package com.four.bmm.controller;

import com.four.bmm.entity.*;
import com.four.bmm.service.NewsService;
import com.four.bmm.util.DateUtil;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/AdminAddNewsServlet")
public class AdminAddNewsServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.取出当前登录用户信息
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("loginAdmin");

        //2.获取主贴表需要的参数:主题、内容、时间
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String currTime = DateUtil.getCurrTime();
        //3.所属版块信息
        int bitemid = Integer.parseInt(request.getParameter("bitemid"));
        System.out.println(bitemid+"-------");
        BlockItem blockitem = new BlockItem();
        blockitem.setBitemid(bitemid);
        //5.把发帖时的参数封装到一个topic对象中
        News news = new News(title, content,null,currTime, admin, blockitem);
        //6.调用service层的方法
        NewsService newsService = new NewsService();
        boolean flag = newsService.setNews(news);
        //7.对service层返回结果进行判断，选择转发页面
        if (flag) {
            response.sendRedirect("back/index.jsp");
        } else {
            response.sendRedirect("back/article-add.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
